package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.PageParam;
import com.example.springboot.common.Result;
import com.example.springboot.entity.RecordNumberPeople;
import com.example.springboot.service.IRecordNumberPeopleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author
 * @Date 2025/4/19
 */

@RestController
@RequestMapping("/recordNumberPeople")
public class RecordNumberPeopleController {
    @Resource
    private IRecordNumberPeopleService recordNumberPeopleService;

    @PostMapping("api/save")
    public Result save(@RequestBody RecordNumberPeople recordNumberPeople){
        recordNumberPeopleService.saveOrUpdate(recordNumberPeople);
        return Result.success(recordNumberPeople);
    }
    @PostMapping("api/del/{id}")
    public Result del(@PathVariable Integer id){
        recordNumberPeopleService.removeById(id);
        return Result.success();
    }
    @GetMapping("page")
    public Result page(PageParam<RecordNumberPeople> pageParam, RecordNumberPeople recordNumberPeople){
        LambdaQueryWrapper<RecordNumberPeople> wrapper = Wrappers.lambdaQuery(recordNumberPeople);
        PageParam<RecordNumberPeople> page = recordNumberPeopleService.page(pageParam, wrapper);
        return Result.success(page);
    }



    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "") Integer age,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<RecordNumberPeople> queryWrapper = new QueryWrapper<RecordNumberPeople>().orderByDesc("id");
        queryWrapper.like(!"".equals(age), "age", age);
        Page<RecordNumberPeople> page = recordNumberPeopleService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    @PostMapping("/getList")
    public Result getList() {
        return Result.success(recordNumberPeopleService.getList());
   }

   @PostMapping("/getDeathOrSurvivalList")
    public Result getDeathOrSurvivalList() {
        return Result.success(recordNumberPeopleService.getDeathOrSurvivalList());
   }

    @PostMapping("/getListAge")
    public Result getListAge() {
        List<RecordNumberPeople> list = recordNumberPeopleService.list();

        List<Integer> ageList = list.stream().map(RecordNumberPeople::getAge).distinct().collect(Collectors.toList());
        return Result.success(ageList);
    }

    @PostMapping("/getListNumberPeople")
    public Result getListNumberPeople() {
        List<RecordNumberPeople> list = recordNumberPeopleService.getList();

        List<Long> numbers = list.stream().map(RecordNumberPeople::getNumber).collect(Collectors.toList());
        return Result.success(numbers);
    }
}
